package com.beyond.trace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.domain.TraceBatch;
import com.beyond.trace.domain.vo.ScreenBatchInfoVo;
import com.beyond.trace.mapper.TraceBatchMapper;
import com.beyond.trace.service.ITraceBatchService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 批次信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceBatchServiceImpl extends ServiceImpl<TraceBatchMapper, TraceBatch> implements ITraceBatchService
{
    @Resource
    private TraceBatchMapper traceBatchMapper;

    /**
     * 查询批次信息
     * 
     * @param batchId 批次信息ID
     * @return 批次信息
     */
    @Override
    public TraceBatch selectTraceBatchById(Long batchId)
    {
        QueryWrapper<TraceBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_id",batchId);
        try {  //解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return traceBatchMapper.selectOne(queryWrapper);
    }

    /**
     * 查询批次信息
     *
     * @param batchNo 批次信息编号
     * @return 批次信息
     */
    public TraceBatch selectTraceBatchByBatchNo(String batchNo){
        QueryWrapper<TraceBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_no",batchNo);
        try {  //解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return traceBatchMapper.selectOne(queryWrapper);
    }

    /**
     * 查询批次信息列表
     * 
     * @param traceBatch 批次信息
     * @return 批次信息
     */
    @Override
    public List<TraceBatch> selectTraceBatchList(TraceBatch traceBatch)
    {
//        try {  //解决未传token的问题
//            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//                traceBatch.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
//            }
//        }catch (Exception e) {
//            e.printStackTrace();
//        }

        return traceBatchMapper.selectTraceBatchList(traceBatch);
    }

    /**
     * 新增批次信息
     * 
     * @param traceBatch 批次信息
     * @return 结果
     */
    @Override
    public boolean insertTraceBatch(TraceBatch traceBatch)
    {
        if(StringUtils.isBlank(traceBatch.getBatchNo())){
            String batchNo = generatorBatch(traceBatchMapper.getMaxBatch());
            Integer traceBatchOne = traceBatchMapper.selectTraceBatchByBatchNo(batchNo);
            while (traceBatchOne != null && traceBatchOne > 0){
                batchNo = generatorBatch(batchNo);
                traceBatchOne = traceBatchMapper.selectTraceBatchByBatchNo(batchNo);
            }
            traceBatch.setBatchNo(batchNo);
        }
        traceBatch.setCreateTime(DateUtils.getNowDate());
        traceBatch.setUpdateTime(DateUtils.getNowDate());
        traceBatch.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceBatch.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceBatch.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return traceBatchMapper.insertTraceBatch(traceBatch);
        return super.save(traceBatch);
    }

    /**
     * 生成批次号
     * @return
     */
    private String generatorBatch(String batchNo){

        String yyyyMMdd = DateUtils.dateTimeNow("yyyyMMdd");
        if(com.ruoyi.common.utils.StringUtils.isEmpty(batchNo) || batchNo.length() <=10) {
            return yyyyMMdd + "001";
        }else {
            try {
                Long old = Long.parseLong(batchNo);
                return String.valueOf(old +1);
            }catch (Exception e) {
                return yyyyMMdd + "001";
            }
        }
    }

    /**
     * 修改批次信息
     * 
     * @param traceBatch 批次信息
     * @return 结果
     */
    @Override
    public int updateTraceBatch(TraceBatch traceBatch)
    {
        traceBatch.setUpdateTime(DateUtils.getNowDate());
        traceBatch.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceBatchMapper.updateById(traceBatch);
    }

    /**
     * 批量删除批次信息
     * 
     * @param batchIds 需要删除的批次信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceBatchByIds(Long[] batchIds)
    {
        QueryWrapper<TraceBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("batch_id", Arrays.asList(batchIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceBatchMapper.delete(queryWrapper);
    }

    /**
     * 删除批次信息信息
     * 
     * @param batchId 批次信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceBatchById(Long batchId)
    {
        QueryWrapper<TraceBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_id", batchId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceBatchMapper.delete(queryWrapper);
    }

    @Override
    public List<ScreenBatchInfoVo> findScreen() {
        return this.traceBatchMapper.findScreen();
    }
}
